<%@ page contentType="text/html; charset=UTF-8" errorPage="error.jsp"%>

<%@ page import="com.javapig.util.*"%>
<%@ page import="com.dd.exception.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dd.*" %>
<%@ page import="java.util.ArrayList" %>

<%@ include file="global.jsp" %>
<%
	//boolean errorEmail = false;
	boolean errorUsername = false;
	boolean errorRealname = false;
	boolean errorNoUserDept = false;
	boolean errorNoPassword = false;
	boolean errorNoConfirmPassword = false;
	boolean errorPasswordsNotEqual = false;

	// error variables from user creation
	boolean errorUserAlreadyExists = false;
	boolean errorNoPermissionToCreate = false;

	// overall error variable
	boolean errors = false;

	// creation success variable:
	boolean success = false;
	
	UserManager tempuser = new UserManager();
	tempuser.getUserCount();
    // get parameters	
	String realname         = ParamUtils.getParameter(request,"realname");
	String username         = ParamUtils.getParameter(request,"username");
	//String userDept         = ParamUtils.getParameter(request, "userDept");
	String password         = ParamUtils.getParameter(request,"password");
	String confirmPassword  = ParamUtils.getParameter(request,"confirmPassword");
	int userDept		= ParamUtils.getIntParameter(request,"ddlxiangmubu",-1);
	
	System.out.println(userDept);
	System.out.println(username);

	boolean doCreate        = ParamUtils.getBooleanParameter(request,"doCreate");

	String [] userTypeName  = {"创建审核人员","创建采购人员"};
	int userType = ParamUtils.getIntParameter(request, "userType", 0);
	
    // trim up the passwords so no one can enter a password of spaces
	if( password != null ) {
		password = password.trim();
		if( password.equals("") ) { password = null; }
	}
	if( confirmPassword != null ) {
		confirmPassword = confirmPassword.trim();
		if( confirmPassword.equals("") ) { confirmPassword = null; }
	}
   
    // check for errors
	if( doCreate ) {

		if( username == null || username.length() <= 0 ) {
			errorUsername = true;
		}
		if( password == null || password.length() <=0 ) {
			errorNoPassword = true;
		}
		if( confirmPassword == null || confirmPassword.length() <= 0) {
			errorNoConfirmPassword = true;
		}
		if( password != null && confirmPassword != null
		    && !password.equals(confirmPassword) )
		{
			errorPasswordsNotEqual = true;
		}
		if ( userDept == -1 ){
		   errorNoUserDept = true;
		}
		errors = errorUsername || errorNoPassword
		         || errorNoConfirmPassword || errorPasswordsNotEqual || errorNoUserDept;
	}

    //UserManager userManager = null;
	if( !errors && doCreate ) {
		// get a user manager to edit user properties
		UserManager userManager = new UserManager();
		try {
			User newUser = userManager.createCaiGouYuan(username,realname,password,userDept);
			success = true;
		}
		catch( UserAlreadyExistsException uaee ) {
			errorUserAlreadyExists = true;
			errorUsername = true;
			errors = true;
		}
	}

    // if a user was successfully created, say so and return (to stop the
	// jsp from executing
	if( success ) {
		response.sendRedirect("UserGeneralInfo.jsp");
		return;
	}
    
    XiangMu xmAllXiangMu = new XiangMu();
    ArrayList AllXiangmus = null;
    int nCount = 0;
    if(xmAllXiangMu != null)
    {
    	AllXiangmus = xmAllXiangMu.GetAllXiangMusOld();
    	if(AllXiangmus != null)
    	{
    		nCount = AllXiangmus.size();
    	}
    }

    // Remove the user in the session (if we come to this page, the sidebar
    // shouldn't show the specific user options).
    session.removeAttribute("admin.sidebar.users.currentUser");
%>

<%  // special onload command to load the sidebar
    //onload = " onload=\"parent.frames['sidebar'].location.href='sidebar.jsp?sidebar=users';\"";
%>
<%@ include file="header.jsp" %>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<p>

<%
    String title = userTypeName[userType];
    String[][] breadcrumbs = {
        {"主页面", "main.jsp"},
        {title, "createUser.jsp"}
    };
    StringBuffer pageGuide = new StringBuffer();
    for (int i=0; i<breadcrumbs.length; i++){
      for(int j=0; j<breadcrumbs[i].length; j++){
          pageGuide.append(breadcrumbs[i][j]+"@");
      }
    }

%>

<jsp:include page="title.jsp" flush="true">
   <jsp:param name="title" value="<%=title%>" />
   <jsp:param name="pageGuide" value="<%=pageGuide%>" />
</jsp:include>


<p>

<%	// print error messages
	if( !success && errors ) {
%>
	<p><font color="#ff0000" size="-1">
	<%	if( errorUserAlreadyExists ) { %>
		用户名 "<%= username %>" 已经存在。请使用其它的用户名。
	<%	} else if( errorNoPermissionToCreate ) { %>
		你没有创建用户的权限。
	<%	} else { %>
		有错误发生。请检查以下字段并重试。
	<%	} %>
	</font><p>
<%	} %>

<p>

<%-- form --%>
<form action="createUser.jsp" method="post" name="createForm">
<input type="hidden" name="doCreate" value="true">

<font size="-1"><b>新用户信息(以下各项都为必填内容)</b></font><p>

<table  cellspacing="0" cellpadding="0" border="1" width="95%" align="right">
<td>
<table width="100%" height="81" border="0" cellpadding="3" cellspacing="1" bgcolor="<%= tblBorderColor %>">

<%-- username --%>
<tr bgcolor="#ffffff">
	<td width="239" bgcolor="<%=tr_bakgroundColor%>"><font size="-1"<%= (errorUsername)?" color=\"#ff0000\"":"" %>>
		用户名 <span id="Label6" style="color:Red;">*</span>
		<br>
	</font>	</td>
	<td width="794"><font color="#FFFFFF">
	  <input type="text" name="username" size="30" value="<%= (username!=null)?username:"" %>">
	</font></td>
</tr>
<tr bgcolor="#ffffff">
	<td bgcolor="<%=tr_bakgroundColor%>"><font size="-1"<%= (errorRealname)?" color=\"#ff0000\"":"" %>>
		真实姓名 <span id="Label6" style="color:Red;">*</span>
		<br>
	</font>	</td>
	<td><font color="#FFFFFF">
	  <input type="text" name="realname" size="30" value="<%= (realname!=null)?realname:"" %>">
	</font></td>
</tr>
<%-- userDept --%>
<tr bgcolor="#ffffff">
	<td bgcolor="<%=tr_bakgroundColor%>"><font size="-1"<%= (errorNoUserDept)?" color=\"#ff0000\"":"" %>>
		项目 <span id="Label6" style="color:Red;">*</span>
		<br>
	</font>	</td>
	 <td style="width: 361px; height: 24px">
                               <font color="#FFFFFF">
                               <select name="ddlxiangmubu" id="ddlxiangmubu" style="width:158px;">
                                 <%
                    				if(nCount > 0)
                    				{
                    	 				Iterator it = AllXiangmus.iterator();
                    	 				while(it.hasNext())
                    	 				{
                    	 					XiangMu mx = (XiangMu)it.next();
                    	 					if(mx != null)
                    	 					{
                    	 	
                 				  %>   
                   					  <option value="<%=mx.getId()%>"><%=mx.getMingCheng()%></option>   
      
                    			<%
                    	 					}
                    	 				}
                    				}
                  				  %>                                                       
                               </select>
                               </font></td>
</tr>
<%-- password --%>
<tr bgcolor="#ffffff">
	<td bgcolor="<%=tr_bakgroundColor%>"><font size="-1"<%= (errorNoPassword||errorPasswordsNotEqual)?" color=\"#ff0000\"":"" %> >
		 口令 <span id="Label6" style="color:Red;">*</span>
		 </font>
		 </td>
	<td><font color="#FFFFFF">
	  <input type="password" name="password" value="" size="20" maxlength="30">
	  </font></td>
</tr>

<%-- confirm password --%>
<tr bgcolor="#ffffff">
	<td bgcolor="<%=tr_bakgroundColor%>"><font size="-1"<%= (errorNoConfirmPassword||errorPasswordsNotEqual)?" color=\"#ff0000\"":"" %>>
		 请再次输入口令  <span id="Label6" style="color:Red;">*</span>
		 </font></td>
	<td><font color="#FFFFFF">
	  <input type="password" name="confirmPassword" value="" size="20" maxlength="30">
	  </font></td>
</tr>
<tr>
	<td align="right">
		<input type="submit" value="创建用户" />
	</td>		
	<td>
		<input type="submit" value="放 弃" onclick="location.href='UserGeneralInfo.jsp';return false;" />
	</td>
</tr>
</table>
</td>
</table>

<br clear="all"><br>


&nbsp;


</form>

<script language="JavaScript" type="text/javascript">
<!--
document.createForm.username.focus();
//-->
</script>

<%@ include file="footer.jsp" %>
